package com.sise.controller;


import com.sise.pojo.Dept;
import com.sise.pojo.Emp;
import com.sise.service.DeptService;
import com.sise.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 彭海霞
 * @since 2020-06-10
 */
@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private DeptService deptService;
    /**
     * 分页查询
     */
    @RequestMapping("/selectEmpAll")
    public String selectEmpAll(Model model) {
        //  参数一：当前页
        //  参数二：页面大小
        //  使用了分页插件之后，所有的分页操作也变得简单的！
        List<Emp> list = empService.list(null);
        model.addAttribute("list", list);
        return "emp-list";
    }
    @RequestMapping("/preAdd")
    public String preAdd(Model model) {
        List<Dept> list = deptService.list(null);
        model.addAttribute("list", list);
        return "emp-add";
    }
    @RequestMapping("/addEmp")
    public String addEmp(Emp emp,Model model) {
        System.out.println("进来了");
        boolean save = empService.save(emp);
        return "forward:/emp/selectEmpAll";
    }
    @RequestMapping("/selectEmpById")
    public String selectEmpById( Long id, Model model) {
        Emp emp = empService.getById(id);
        model.addAttribute("emp", emp);
        List<Dept> list = deptService.list(null);
        model.addAttribute("list", list);
        return "emp-view";
    }
    @RequestMapping("/updateEmp")
    public String updateEmp(Emp emp) {
        boolean b = empService.updateById(emp);
        return "forward:/emp/selectEmpAll";
    }
    @RequestMapping("/deleteEmpById")
    public String deleteEmpById(Long id) {
        System.out.println(id);
        boolean b = empService.removeById(id);
        System.out.println(b);
        return "forward:/emp/selectEmpAll";
    }

}

